package handler

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"github.com/gocolly/colly"
	"log"
	"sbd_fldj/models"
	"sbd_fldj/service"
	"strings"
)

type CompanyHandler struct {
	CompanySrv service.CompanyService
}

func (handler CompanyHandler) InsterCompanyHandler(isCompany models.Company) {
	// fmt.Println(id)
	url := fmt.Sprintf("http://202.127.42.127/moafer/listVive/%s.htm", isCompany.Vive)

	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=4F588DC05BDBF3D2C3C5F3873862B72D; wdcid=3324e83c4b4833d9; _gscu_1942319172=62363514zs207j20; _gscbrs_1942319172=1; wdses=11eab50dfb9f14b8; _gscs_1942319172=t624320311h9mmx20|pv:10; wdlast=1662433084")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		nodes := htmlquery.FindOne(doc, `/html/body/div[4]/table/tbody/tr[9]/td[2]`)

		isCompany.Yxjz = htmlquery.InnerText(nodes)
		handler.CompanySrv.InsterCompanySrv(isCompany)
	})
	c.Visit(url)
}
